package com.jic.order.controller.manage;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.order.entity.OrderManualDelivery;
import com.jic.order.manage.request.OrderManualDeliveryRequest;
import com.jic.order.manage.response.OrderManualDeliveryResponse;
import com.jic.order.service.OrderManualDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by HeJun on 2020/5/28
 *
 * @Description:
 */
@Api(tags = "手动转交货")
@RestController
@RequestMapping("/orderManualDelivery")
public class OrderManualDeliveryController {

  @Autowired
  RedisClientTemplate redisClientTemplate;

  @Autowired
  OrderManualDeliveryService orderManualDeliveryService;

  @ApiOperation(value = "获取手动转交货信息分页")
  @PostMapping("/queryPage")
  @ResponseBody
  public RestResult<PageResult<OrderManualDeliveryResponse>> queryPage(@RequestBody OrderManualDeliveryRequest odr) {
    OrderManualDelivery orderManualDelivery = new OrderManualDelivery();
    BeanUtils.copyProperties(odr, orderManualDelivery);

    String userStr = redisClientTemplate.get(odr.getToken());
    Map sysUser = JSONArray.parseObject(userStr, Map.class);
    orderManualDelivery.setTransferToMerchantId(Long.valueOf(sysUser.get("merchantId").toString()));

    Page page = new Page();
    page.setStart(odr.getStart());
    page.setLimit(odr.getLimit());

    return orderManualDeliveryService.queryPage(orderManualDelivery, page);
  }

  @ApiOperation(value = "获取手动转交货信息")
  @PostMapping("/query")
  @ResponseBody
  public RestResult<List<OrderManualDeliveryResponse>> query(@RequestBody OrderManualDeliveryRequest request) {
    OrderManualDelivery orderManualDelivery = new OrderManualDelivery();
    BeanUtils.copyProperties(request, orderManualDelivery);
    return orderManualDeliveryService.query(orderManualDelivery);
  }

  @ApiOperation(value = "修改手动转交货信息")
  @PostMapping("/update")
  @ResponseBody
  public RestResult update(@RequestBody OrderManualDeliveryRequest odr) {
    OrderManualDelivery orderManualDelivery = new OrderManualDelivery();
    BeanUtils.copyProperties(odr, orderManualDelivery);
    return orderManualDeliveryService.update(orderManualDelivery);
  }

  @ApiOperation(value = "新增手动转交货信息")
  @PostMapping("/insert")
  @ResponseBody
  public RestResult insert(@RequestBody OrderManualDeliveryRequest request) {
    return orderManualDeliveryService.insert(request);
  }
}

